//
//  OASendApplyClassViewController.m
//  ERP
//
//  Created by goldeney on 15/3/19.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "OASendApplyClassViewController.h"

#import "MJRefresh.h"
#import "ServiceConnect.h"

#import "OASendApplyClassItemViewCell.h"
#import "OASendApplyClassViewCell.h"

#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"

@interface OASendApplyClassViewController ()<SKSTableViewDelegate>

@property (strong, nonatomic) NSArray *reslutData;

@property (strong, nonatomic) UIActivityIndicatorView *indicatoView;
@property (strong, nonatomic) UIButton *promptButton;

@end

@implementation OASendApplyClassViewController
@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"请选择模板"];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(selectSelector:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.reslutData = [NSArray array];
    
    self.tableView.SKSTableViewDelegate = self;
    self.tableView.shouldExpandOnlyOneCell = YES;
    self.tableView.tableFooterView= [[UIView alloc] init];
    
    CGFloat width = SuperWidth;
    CGFloat height = SuperHeight - self.view.frame.origin.x;
    
    self.indicatoView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _indicatoView.center = CGPointMake(width/2, height/2);
    _indicatoView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
    //_indicatoView.hidesWhenStopped = YES;
    [self.view addSubview:_indicatoView];
    
    self.promptButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [_promptButton setTitle:@"申请模板加载失败，点击重试" forState:UIControlStateNormal];
    [_promptButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    _promptButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_promptButton addTarget:self action:@selector(promptSender:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_promptButton];
    
    //获取网络数据
    [self getApplyClassNetWorkData];
}

- (void)getApplyClassNetWorkData
{
    self.promptButton.hidden = YES;
    self.indicatoView.hidden = NO;
    [self.indicatoView startAnimating];
    
    ServiceConnect *content = [[ServiceConnect alloc] initWithMethodName:@"OA_getApprovalTemplateType" dataArray:nil];
    [content startConnectNetworkCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        if (result.resultArray && result.resultArray.count>0) {
            self.reslutData = result.resultArray;
        } else {
            self.promptButton.hidden = NO;
        }
        self.indicatoView.hidden = YES;
        [self.indicatoView stopAnimating];
        [self.tableView refreshData];
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        self.promptButton.hidden = NO;
        self.indicatoView.hidden = YES;
        [self.indicatoView stopAnimating];
        [self.tableView refreshData];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        self.promptButton.hidden = NO;
        self.indicatoView.hidden = YES;
        [self.indicatoView stopAnimating];
        [self.tableView refreshData];
    }];
}

- (void)promptSender:(id)sender
{
    //获取合同数据
    [self getApplyClassNetWorkData];
}


#pragma mark -获取当前其他数据

- (void)getOtherListData:(NSDictionary*)dictionary
{
    NSNumber *templateId = [dictionary objectFormatForKey:@"id"];
    ServiceParametModel *model = [[ServiceParametModel alloc] init];
    [model setValue:templateId forKey:@"templateId"];
    
    ServiceConnect *content = [[ServiceConnect alloc] initWithMethodName:@"OA_getProcessByTemplateId" dataArray:model];
    [content showMBProgressHUD:self.navigationController labelText:@"正在处理"];
    [content startConnectNetworkCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if (result.resultArray && result.resultArray.count>0) {
            NSArray *resultValue = @[dictionary,result.resultArray];
            //关闭页面，返回数据
            if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)])
                [self.backResultDelagate backResultDataKey:self.key dataValue:resultValue];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self showErrorHUDText:@"处理失败"];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showErrorHUDText:@"处理失败"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showErrorHUDText:@"网络异常"];
    }];
    
}

#pragma mark - UIBarButtonItem Event

- (void)selectSelector:(id)selector
{
    
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.reslutData.count;
}

- (NSInteger)tableView:(SKSTableView *)tableView numberOfSubRowsAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *mDictionary = [self.reslutData objectAtIndex:indexPath.row];
    NSArray *itemArray = [mDictionary objectFormatForKey:@"template"];
    return itemArray.count;
}

- (UITableViewCell *)tableView:(SKSTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"OASendApplyClassViewCellClass";
    OASendApplyClassViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
        cell = [[OASendApplyClassViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    [cell setIsShowAccessory:YES];
    
    NSDictionary *mDictionary = [self.reslutData objectAtIndex:indexPath.row];
    cell.titleLable.text = [mDictionary objectFormatStringForKey:@"mc"];
    return cell;
}

- (UITableViewCell *)tableView:(SKSTableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"OASendApplyClassItemViewCell";
    OASendApplyClassItemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
        cell = [[OASendApplyClassItemViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    NSDictionary *mDictionary = [self.reslutData objectAtIndex:indexPath.row];
    NSArray *itemArray = [mDictionary objectFormatForKey:@"template"];
    NSDictionary *itemValue = [itemArray objectAtIndex:indexPath.subRow-1];
    
    cell.titleLable.text = [itemValue objectFormatStringForKey:@"templatename"];
    return cell;
    
}

- (CGFloat)tableView:(SKSTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(SKSTableView *)tableView heightForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Section: %ld, Row:%ld, Subrow:%ld", (long)indexPath.section, indexPath.row, (long)indexPath.subRow);
}

- (void)tableView:(SKSTableView *)tableView didSelectSubRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Section: %ld, Row:%ld, Subrow:%ld", indexPath.section, (long)indexPath.row, (long)indexPath.subRow);
    
    NSDictionary *mDictionary = [self.reslutData objectAtIndex:indexPath.row];
    NSArray *itemArray = [mDictionary objectFormatForKey:@"template"];
    NSDictionary *itemValue = [itemArray objectAtIndex:indexPath.subRow-1];
    [self getOtherListData:itemValue];
}

@end
